import axios from "axios";
import { Toast } from 'vant';
import {removeToken } from "./storage";
import router from "@/router";
const request = axios.create({
  baseURL: 'http://liufusong.top:8080/',
  timeout: 7000
})

// 添加响应拦截器
axios.interceptors.response.use(
  function (response) {
    const { data } = response
    return data
  },
  function (error) {
    if (error.response) {
      if (error.response.status === 401) {
        Toast.error('尊敬的用户，当前登录状态已过期！')
        removeToken()
        router.push('/login')
      } else {
        Toast.error(error.response.data.message)
      }
    }
    return Promise.reject(error)
  }
)

// 添加请求拦截器
request.interceptors.request.use(
  function (config) {
    // const token=getToken()
    // config.headers.Authorization = `Bearer ${token}`
    config.headers.Authorization=localStorage.getItem('my_token')
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

export default request